/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-09-15 14:14:23
 * @filename : static/logic/nmm/src/views/upgrade.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){
    require('{controller}messages');
    require('{component}promptTip/promptTip.css');
    require("{component}dialog/zepto.dialog.css");

    var messageListView,userInfo,Message,PMSG,bool=true,
        mId = 'messageList',reRender = false,menuName = 'message',
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        panel = require('{component}panel/zepto.panel'),
        promptTip = require('{component}promptTip/promptTip'),
        dialog = require('{component}dialog/zepto.dialog'),
        swipe = require('{component}swipe/zepto.swipe'),
        tpl = require('{view}messageList.tpl'),
        tplN = require('{view}userNav.tpl');

    messageListView = Backbone.View.extend({

        template: _.template(tpl),
        templateN: _.template(tplN),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            if ($('#'+mId).length < 1) {
                $('body').append(this.template(lang));
                this.bindEvt();
                userInfo = share.userInfo();
                share.PMSG = new share.message();
                share.PMSG.init();
                share.PMSG.opt.addUserToList = this.addUserToList;
            } else {
                $('#'+mId).css('display','block');
            }
            share.panel(panel,this.templateN,mId,menuName);
        },
    
        openDialog : function() {
            dialog.open({
                type : 'payDialog',
                dom : $('#'+mId).find('.body_all'),
                cover : true,
                positionRelative : true
            });
        },
        
        bindEvt : function() {
            var tthis = this,tempId,dom = $('#'+mId);
           
            $(window).bind('orientationchange.messageList',function(){
                var pW = $('#'+mId).width();
                dom.find('.info').css('width',pW-100+'px');
            });

            dom.find('.messagelist').swipe({
                 swipeLeft : function(e,touch) { 
                    if (Math.abs(touch.MY) < 50) {
                        var dom = $(e.target).closest('li');
                            $(dom).find('.messagebox').css({               
                                '-webkit-transform' : 'translate(-100px)', 
                                '-webkit-transition' : '200ms linear',
                                '-moz-transform' : 'translate(-100px)', 
                                '-moz-transition' : '200ms linear'
                            }); 
                            $(dom).attr('touch',false);
                    }
                 },
                 swipeRight : function(e,touch) {
                    if (Math.abs(touch.MY) < 50) {
                         var dom = $(e.target).closest('li');
                             $(dom).find('.messagebox').css({                
                                '-webkit-transform' : 'translate(0px)', 
                                '-webkit-transition' : '200ms linear',
                                '-moz-transform' : 'translate(0px)', 
                                '-moz-transition' : '200ms linear'
                             });
                             $(dom).attr('touch',false);//兼容
                             setTimeout(function(){
                                 (function(){
                                   $(dom).attr('touch',true);
                                 })();
                             },1000);
                    }
                 }
            });

            dom.find('.right').on('tap',function(){
                    var messageDom = dom.find('.messagesearch');
                    if (messageDom.css('display') == 'none') {
                        messageDom.show();
                    } else {
                        messageDom.hide();
                    }
            });           
 
            dom.find('.left').on('tap',function(e){
                e.stopPropagation();
                if (panel.info.state == 'hidden') {
                    panel.show();
                } else {
                    panel.hidden(); 
                }
            }); 

            dom.find('.messagelist').on('tap',function(e){
                var dom = $(e.target).closest('.del');
                if (dom.hasClass('del')) {
                   share.PMSG.removeArchive($(dom).parents('li').attr('username')); 
                   $(dom).parents('li').remove();
                }               
            });

            dom.find('.messagelist').on('tap',function(e){
                    var dom = $(e.target).closest('li'),
                        id = dom.attr('uid'),
                        userName = dom.attr('userName');

                if ($(dom).attr('touch') !== 'false') { 
                    dom.find('.num').html('').hide();  

                    if (userName == userInfo.username) {
                        return;
                    }
                    window.location.href = '#messageConversation/whole/custom'+userName+'&'+id;
                }
            });

            dom.find('.sendButton').on('tap',function(){ 
                var val = dom.find('.sendBox').find('input').val(),
                    obj = share.getObjStorage('messageList');

                if ($.trim(val) != '') {
                    tthis.searchMessageUser($.trim(val),obj);
                } else {
                    tthis.addUserToList(obj); 
                }

                var messageDom = dom.find('.messagesearch');
                messageDom.hide();
            });
        },

        searchMessageUser : function(keyword,obj) {
            var arr=[];            
            for (var i in obj) {
                 var keyword = keyword.toLowerCase(),
                     name = obj[i].name.toLowerCase();
                 if (name.indexOf(keyword) > -1) { 
                     arr.push(obj[i]);
                 }
            }
            this.addUserToList(arr); 
        },

        addUserToList : function(userArr) {
            var dom = $('#'+mId).find('.messagelist');
                dom.html('');
            if (userArr.length > 0) {
                for (var i in userArr) {
                     var user = userArr[i],
                         tplM = require('{view}messageListLi.tpl'),
                         templateM = _.template(tplM);

                     user.defaultImg = seajs.data.vars.resourceUrl+'img/';
                     if (user.name == userInfo.username) {
                         dom.prepend(templateM(user));
                     } else {
                         dom.append(templateM(user));
                     }
                }
            } else {
                promptTip.show({type:'noData',text:'There are no new messages.', dom:dom});
            }
            dom.imglazyload({startY : 0,endY: dom.height()+200}); 
            var pW = $('#'+mId).width();
            dom.find('.info').css('width',pW-100+'px');
        }
    });
    module.exports = messageListView;
});
